java使用javax.mail包发送电子邮件:设置账号、密码、主题、文本、附件

您所在的位置:网站首页 java email发送 java使用javax.mail包发送电子邮件:设置账号、密码、主题、文本、附件

java使用javax.mail包发送电子邮件:设置账号、密码、主题、文本、附件

2023-03-15 19:30| 来源: 网络整理| 查看: 265

package com.lp.app.net;

import java.util.*;import java.io.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;

String to = "";//收件人String from = "";//发件人String host = "";//smtp主机String username = "" ;String password = "" ;String filename = "";//附件文件名String subject = "";//邮件主题String content = "";//邮件正文Vector file = new Vector();//附件文件集合

public Mail(){}//构造器,提供直接的参数传入public Mail(String to,String from,String smtpServer,String username,String password,String subject,String content){ this.to = to; this.from = from; this.host = smtpServer; this.username = username; this.password = password; this.subject = subject; this.content = content;}//设置邮件服务器地址public void setHost(String host){ this.host = host;}//设置登录服务器校验密码public void setPassWord(String pwd){ this.password = pwd;}//设置登录服务器校验用户public void setUserName(String usn){ this.username = usn;}//设置邮件发送目的邮箱public void setTo(String to){ this.to = to;}//设置邮件发送源邮箱public void setFrom(String from){ this.from = from;}//设置邮件主题public void setSubject(String subject){ this.subject = subject;}//设置邮件内容public void setContent(String content){ this.content = content;}//把主题转换为中文public String transferChinese(String strText){ try{ strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B"); }catch(Exception e){ e.printStackTrace(); } return strText;}//往附件组合中添加附件public void attachfile(String fname){ file.addElement(fname);}//发送邮件public boolean sendMail(){

//构造mail session Properties props = System.getProperties(); props.put("mail.smtp.host",host); props.put("mail.smtp.auth","true"); Session session=Session.getDefaultInstance(props, new Authenticator(){ public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(username,password); } });

try { //构造MimeMessage 并设定基本的值 MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address={new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO,address); subject = transferChinese(subject); msg.setSubject(subject);

//构造Multipart Multipart mp = new MimeMultipart();

//向Multipart添加正文 MimeBodyPart mbpContent = new MimeBodyPart(); mbpContent.setText(content); //向MimeMessage添加(Multipart代表正文) mp.addBodyPart(mbpContent);

//向Multipart添加附件 Enumeration efile=file.elements(); while(efile.hasMoreElements()){

MimeBodyPart mbpFile = new MimeBodyPart(); filename=efile.nextElement().toString(); FileDataSource fds = new FileDataSource(filename); mbpFile.setDataHandler(new DataHandler(fds)); mbpFile.setFileName(fds.getName()); //向MimeMessage添加(Multipart代表附件) mp.addBodyPart(mbpFile);

}

file.removeAllElements(); //向Multipart添加MimeMessage msg.setContent(mp); msg.setSentDate(new Date()); //发送邮件 Transport.send(msg);

} catch (MessagingException mex) { mex.printStackTrace(); Exception ex = null; if ((ex=mex.getNextException())!=null){ ex.printStackTrace(); } return false; } return true; }

}



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3